package com.example.shortmessage.base;

import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewbinding.ViewBinding;



/**
 * @author 27638
 *
 */
public abstract class BaseActivity<T extends ViewBinding, V extends IBaseView<P>, P extends IBasePresenter> extends AppCompatActivity {
    protected T mBinding;
    protected String TAG = BaseActivity.class.getSimpleName();
    protected P mPresenter;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = getLayoutView();
        setContentView(mBinding.getRoot());
        setWindows();
        attachPresenter();
        setStyle();
        initView();
    }

    /**
     * 设置透明状态栏导航栏
     */

    private void setWindows(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            Window window = getWindow();
            window.setDecorFitsSystemWindows(false);
        }else {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
    }

    /**
     * 添加Present相关
     */
    private void attachPresenter() {
        ((V) this).setPresenter(initPresenter());
        if (mPresenter != null) {
            mPresenter.attachView((V) this);
        }
    }
    /**
     * Present
     * @return
     */
    protected abstract P initPresenter();

    /**
     * 初始化View Binding
     * @return
     */
    protected abstract T getLayoutView();

    /**
     * onCreate方法中运行
     */
    protected abstract void initView();

    /**
     * onCreate方法中运行
     * 设置样式
     */
    protected abstract void setStyle();

    /**
     * 禁止跟随系统字体大小
     * @return
     */
    @Override
    public Resources getResources() {
        Resources resources = super.getResources();
        Configuration configuration = resources.getConfiguration();
        configuration.fontScale = 1f;
        return this.createConfigurationContext(configuration).getResources();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.detachView();
            mPresenter = null;
        }
    }
}